package com.sunshine.ssm.job;

import org.apache.commons.lang3.time.FastDateFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
 * @Author: WeiHong
 * @Date: 2017/5/19 9:45
 */
public abstract class AbstractJob {
    private static final Logger LOGGER = LoggerFactory.getLogger(AbstractJob.class);

    private String jobName;

    /**
     * 构造方法
     *
     * @param jobName 任务名称
     */
    public AbstractJob(String jobName){
        this.jobName = jobName;
    }

    /**
     * 干定时任务
     */
    protected void doJob(){
        LOGGER.debug("任务[{}]在[{}]开始运行", jobName, FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()));
        try {
            doTask();
        } catch (Exception e) {
            LOGGER.error("任务[{}]执行错误", jobName, e);
        }
        LOGGER.debug("任务[{}]在[{}]结束运行", jobName, FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()));
    }

    /**
     * 真正的千定时任务
     * @throws Exception
     */
    protected abstract void doTask() throws Exception;
}
